package chapter12;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

/**
 * @author : DuJiabao
 * @Date : 2021/2/20 11:02
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class ExceptionLoggerTest {
  public static void main(String[] args) {
    try {
      throw new LoggingException();
    } catch (LoggingException e) {
      System.err.println("Caught" + e);
    }
  }
}

class LoggingException extends Exception {
  private static Logger logger = Logger.getLogger("LoggingException");

  public LoggingException() {
    // StringWriter将包含堆栈信息
    StringWriter trace = new StringWriter();
    //必须将StringWriter封装成PrintWriter对象，
    //以满足printStackTrace的要求
    printStackTrace(new PrintWriter(trace));
    logger.severe(trace.toString());
  }
}
